﻿using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using System;
using UnityEngine.Events;
using System.IO;
public class TjSdk:MonoSingleton<TjSdk>
{
	public Texture2D ShareImage;
	
	void Start ()
	{
		TjSdk.Init ();
	}
	
	
	#if applovin
	
	public void onAppLovinEventReceived (string ev)
	{
		
		ApplovinAds.GetInstance ().AdsCallback (ev);
		
	}
	#endif
	
	#if gdt
	/// <summary>
	/// 广电通回调
	/// </summary>
	/// <param name="ev">Ev.</param>
	public void onGdtEventReceived (string ev)
	{
		GdtAds.GetInstance ().AdsCallback (ev);
	}
	#endif
	
	public static void ShowBanner (bool show)
	{

		#if IOSInner || IOSTaiwan
		AliAds.GetInstance ().PlayBanner (show);
		#endif 
	}
	
	public static void ShowIncentiveVideo (Action ac = null)
	{
		#if UNITY_EDITOR 
		Debug.Log ("YXC  " + " ShowIncentiveVideo");
		ac ();
		#endif 
		
		#if applovin
		if (ApplovinAds.GetInstance ().IsIncentivizedAvailable ()) {
			ApplovinAds.GetInstance ().ShowIncentVideo (ac);
		} else {
			if (VungleAds.GetInstance ().IsIncentivizedAvailable ()) {
				VungleAds.GetInstance ().ShowIncentVideo (ac);
			} 
		}
		#endif 
	}
	
	public static void ShowInterVideo (Action ac = null)
	{
		#if UNITY_EDITOR 
		Debug.Log (" ShowInterVideo");
		if (ac != null) {
			ac ();
		}
		return;
		#endif 
		
		#if gdt
		GdtAds.GetInstance ().ShowInterVideo (ac);
		#elif applovin
		if (ApplovinAds.GetInstance ().IsInterstitialAvailable ()) {
			ApplovinAds.GetInstance ().ShowInterVideo (ac);
		} else {
			if (VungleAds.GetInstance ().IsInterstitialAvailable ()) {
				VungleAds.GetInstance ().ShowInterVideo (ac);
			} 
		}
		#endif 
	}
	
	public static bool IsIncentivizedAvailable ()
	{
		#if UNITY_EDITOR 
		return true;
		#endif 
		#if applovin
		if (ApplovinAds.GetInstance ().IsIncentivizedAvailable ())
			return true;
		if (VungleAds.GetInstance ().IsIncentivizedAvailable ())
			return true;
		#endif 
		return false;
		
	}
	
	public static bool IsInterstitialAvailable ()
	{
		#if UNITY_EDITOR 
		return true;
		#endif 
		#if gdt
		return GdtAds.GetInstance ().IsInterstitialAvailable ();
		#endif
		
		#if applovin 
		if (ApplovinAds.GetInstance ().IsInterstitialAvailable ())
			return true;
		if (VungleAds.GetInstance ().IsInterstitialAvailable ())
			return true;
		#endif  
		return false;
	}
	
	public static void Init ()
	{

		Debug.Log ("YXC" + "  Init all  ");
		YxcAnalyze.AnalyzeInit ();
		Debug.Log ("YXC" + "  YxcAnalyze Init complete  ");
		#if !UNITY_EDITOR
		UmengShare.InitUmengKey (Myparameters.QqKey,
		                         Myparameters.QqSecret,
		                         Myparameters.WeixinKey,
		                         Myparameters.WeixinSecret,
		                         Myparameters.SinaKey,
		                         Myparameters.SinaSecret);
		Debug.Log ("YXC" + "  UmengShare Init complete  ");
		#endif
		#if IOSInner || IOSTaiwan
		AliAds.GetInstance ().Init (Myparameters.AlimamKey);
		#endif
		#if applovin
		ApplovinAds.GetInstance ().Init (Myparameters.Applovin_key, "TjSdk");
		Debug.Log ("YXC" + "  ApplovinAds Init complete  ");
		VungleAds.GetInstance ().Init (Myparameters.VungleAndroidKey, Myparameters.VungleiOSKey);
		Debug.Log ("YXC" + "  VungleAds Init complete  ");
		#endif  
		
		#if gdt
		GdtAds.GetInstance ().Init (Myparameters.GdtAppId, Myparameters.GdtInterKey);
		Debug.Log ("YXC" + "  GdtAds Init complete  ");
		#endif
		
	}
	
//	void OnGUI ()
//	{
//		
//		GUILayout.BeginVertical ("box");
//		if (GUILayout.Button ("IsInterstitialAvailable", GUILayout.Width (Screen.width / 2), GUILayout.Height (Screen.height / 10))) {
//			
//			IsInterstitialAvailable ();
//			
//		}
//		if (GUILayout.Button ("GetInterstitial", GUILayout.Width (Screen.width / 2), GUILayout.Height (Screen.height / 10))) {
//			
//			GdtAds.GetInstance ().GetInterstitial ();
//		}
//		if (GUILayout.Button ("ShowInterVideo", GUILayout.Width (Screen.width / 2), GUILayout.Height (Screen.height / 10))) {
//			ShowInterVideo ();
//			
//		}
//		if (GUILayout.Button ("OpenSharePlateform", GUILayout.Width (Screen.width / 2), GUILayout.Height (Screen.height / 10))) {
//			
//			UmengShare.OpenSharePlateform ("content", Application.persistentDataPath + "/Sceenshot.png", "title", "http://www.baidu.com");
//			
//		}
//		if (GUILayout.Button ("OpenShareImageOnly", GUILayout.Width (Screen.width / 2), GUILayout.Height (Screen.height / 10))) {
//			
//			UmengShare.OpenShareImageOnly (Application.persistentDataPath + "/Sceenshot.png");
//			
//		}
//		if (GUILayout.Button ("4399Pay", GUILayout.Width (Screen.width / 2), GUILayout.Height (Screen.height / 10))) {
//			#if ssjj
//			M4399_Pay ("1", "ssss");
//			Debug.Log ("YXC" + "  complete call android  ");
//			#endif
//		}
//		GUILayout.EndVertical ();
//	}
	
	#if ssjj
	#region 4399相关
	/// <summary>
	/// 4399的支付
	/// </summary>
	/// <param name="moneyAmount">支付金额(元)</param>
	/// <param name="productName">显示在支付页面的商品名称</param>
	public static void M4399_Pay (string moneyAmount, string productName)
	{
		using (AndroidJavaClass jc = new AndroidJavaClass("com.unity3d.player.UnityPlayer"))
		using (AndroidJavaObject jo = jc.GetStatic<AndroidJavaObject>("currentActivity")) {
			jo.Call ("Pay", moneyAmount, productName);
		}
	}
	/// <summary>
	/// 4399支付回调
	/// </summary>
	/// <param name="result">0支付成功 1支付异常</param>
	public void OnPayResult (string result)
	{
		//支付成功处理事件
		if ("0" == result) {
			ShopMgr.PayCallBack (0);
			Debug.Log ("YXC" + "  zhifuchengggong  ");
		}
		//支付失败处理事件
		else if ("1" == result) {
			Debug.Log ("YXC" + "  zhifushibai  ");
			ShopMgr.PayCallBack (1);
		}
	}
	#endregion
	
	#endif
}